
import * as tf from '@tensorflow/tfjs'

// 预处理图片函数（组件外部的工具函数）
export const preprocessImage = (img) => {
  const canvas = document.createElement('canvas')
  canvas.width = 128
  canvas.height = 128

  const ctx = canvas.getContext('2d')
  if (!ctx) return canvas

  // 保持比例缩放并居中裁剪
  const ratio = Math.min(128 / img.width, 128 / img.height)
  const newWidth = img.width * ratio
  const newHeight = img.height * ratio

  ctx.drawImage(
    img,
    (128 - newWidth) / 2,
    (128 - newHeight) / 2,
    newWidth,
    newHeight
  )
  return canvas
}


